package com.lion.tag.input;

import java.io.IOException;

import javax.servlet.jsp.JspException;

import com.lion.tag.LionBaseTag;
import com.lion.tag.util.AppendUtil;

public class ComboBox extends LionBaseTag {
	private static final long serialVersionUID = -7513113546435098358L;
	private String id;
	private String name;
	private String style;
	private String multiple;//是否多选
	private String url;
	private String valueField;// 值主键
	private String textField;// 文本主键
	private String panelWidth;// 选项宽度
	private String panelHeight;// 选项高度
	private String formatter;// 格式化函数

	@Override
	public int doStartTag() throws JspException {
		StringBuilder sb = new StringBuilder(50);
		if (url == null || url.equals("")) {
			sb.append("<select ");
			sb = AppendUtil.addAttribute(sb, "id", id);
			sb = AppendUtil.addAttribute(sb, "class", "easyui-combobox");
			sb = AppendUtil.addAttribute(sb, "name", name);
			sb = AppendUtil.addAttribute(sb, "style", style);
			sb.append("data-options=\"");
			if (multiple != null && !multiple.equals("")) {
				sb.append("multiple:'");
				sb.append(multiple);
				sb.append("'");
			}
			sb.append("\"");
			sb.append(">");
			try {
				pageContext.getOut().write(sb.toString());
			} catch (IOException e) {
				e.printStackTrace();
			}
		} else {
			sb.append("<input ");
			sb = AppendUtil.addAttribute(sb, "id", id);
			sb = AppendUtil.addAttribute(sb, "class", "easyui-combobox");
			sb = AppendUtil.addAttribute(sb, "name", name);
			sb = AppendUtil.addAttribute(sb, "style", style);
			sb.append("data-options=\"");
			int hasData = 0;
			if (multiple != null && !multiple.equals("")) {
				hasData = 1;
				sb.append("multiple:'");
				sb.append(multiple);
				sb.append("'");
			}
			if (url != null && !url.equals("")) {
				if (hasData == 1) {
					sb.append(",");
				} else {
					hasData = 1;
				}
				sb.append("url:'");
				sb.append(getContextPath() + url);
				sb.append("'");
			}
			if (valueField != null && !valueField.equals("")) {
				if (hasData == 1) {
					sb.append(",");
				} else {
					hasData = 1;
				}
				sb.append("valueField:'");
				sb.append(valueField);
				sb.append("'");
			}
			if (textField != null && !textField.equals("")) {
				if (hasData == 1) {
					sb.append(",");
				} else {
					hasData = 1;
				}
				sb.append("textField:'");
				sb.append(textField);
				sb.append("'");
			}
			if (panelWidth != null && !panelWidth.equals("")) {
				if (hasData == 1) {
					sb.append(",");
				} else {
					hasData = 1;
				}
				sb.append("panelWidth:'");
				sb.append(panelWidth);
				sb.append("'");
			}
			if (panelHeight != null && !panelHeight.equals("")) {
				if (hasData == 1) {
					sb.append(",");
				} else {
					hasData = 1;
				}
				sb.append("panelHeight:'");
				sb.append(panelHeight);
				sb.append("'");
			}
			if (formatter != null && !formatter.equals("")) {
				if (hasData == 1) {
					sb.append(",");
				} else {
					hasData = 1;
				}
				if (formatter.indexOf("(") == -1) {
					formatter = formatter.split("(")[0];
				}
				sb.append("formatter:'");
				sb.append(formatter);
				sb.append("'");
			}
			sb.append("\"");
			try {
				pageContext.getOut().write(sb.toString());
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return EVAL_BODY_INCLUDE;// 表示标签之间的内容被正常执行
	}

	@Override
	public int doEndTag() throws JspException {
		try {
			if (url == null || url.equals("")) {
				pageContext.getOut().write("</select>");
			} else {
				pageContext.getOut().write("/>");
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return EVAL_PAGE;
	}

	@Override
	public String getId() {
		return id;
	}

	@Override
	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Override
	public String getStyle() {
		return style;
	}

	@Override
	public void setStyle(String style) {
		this.style = style;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getValueField() {
		return valueField;
	}

	public void setValueField(String valueField) {
		this.valueField = valueField;
	}

	public String getTextField() {
		return textField;
	}

	public void setTextField(String textField) {
		this.textField = textField;
	}

	public String getPanelWidth() {
		return panelWidth;
	}

	public void setPanelWidth(String panelWidth) {
		this.panelWidth = panelWidth;
	}

	public String getPanelHeight() {
		return panelHeight;
	}

	public void setPanelHeight(String panelHeight) {
		this.panelHeight = panelHeight;
	}

	public String getFormatter() {
		return formatter;
	}

	public void setFormatter(String formatter) {
		this.formatter = formatter;
	}

	public String getMultiple() {
		return multiple;
	}

	public void setMultiple(String multiple) {
		this.multiple = multiple;
	}

}
